package com.ebt.m.policy.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.data.rxModel.apibean.PolicyProposalItem;
import com.ebt.m.utils.DateUtils;
import com.ebt.m.utils.NumberHelper;
import com.ebt.m.utils.imageloader.ImageLoader;
import com.ebt.m.utils.imageloader.ImageLoaderUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PolicyProposalItemView extends IView implements View.OnClickListener {

    private Context mContext;
    @BindView(R.id.tv_import)
    TextView tv_import;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_coverage)
    TextView tv_coverage;
    @BindView(R.id.tv_premium)
    TextView tv_premium;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.iv_image)
    ImageView iv_image;
    @BindView(R.id.ll_content)
    LinearLayout ll_content;
    PolicyProposalItem item;
    //投保人姓名
    String applicantName;
    public PolicyProposalItemView(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... args) {
        try {
            item = (PolicyProposalItem) args[0];
            applicantName = (String) args[1];
            tv_name.setText(item.name);
            tv_date.setText(DateUtils.dateTime2String(DateUtils.stringToDateTime(item.createTime), DateUtils.YYYY_MM_DD_HH_MM));
            tv_coverage.setText("保额" + NumberHelper.formatDouble(Double.valueOf(item.coverage)).toString() + "元");
            //处理脏数据，有的带"元"，有的不带"元"
            if (item.premium.contains("元")) {
                item.premium = item.premium.replace("元", "");
            }
            tv_premium.setText("保费" + NumberHelper.formatDouble(Double.valueOf(item.premium)).toString() + "元");
            ImageLoaderUtil.getInstance().loadImage(getContext(), new ImageLoader.Builder().url(item.thumbnail).imgView(iv_image).build());
        } catch (Exception e) {
            Logger.e(e);
        }
    }

    public PolicyProposalItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PolicyProposalItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        View contentView = inflate(context, R.layout.view_policy_proposal_item, this);
        ButterKnife.bind(this);
        tv_import.setOnClickListener(this);
        ll_content.setOnClickListener(this);
    }
//    private String getStatusName(int status) {
//        String statusName = "";
//        switch (status) {
//            case 1:
//                statusName = "草稿";
//                break;
//            case 2:
//                statusName = "未发送";
//                break;
//            case 3:
//                statusName = "已发送";
//                break;
//            case 4:
//                statusName = "已查看";
//                break;
//        }
//        return statusName;
//    }

    @Override
    public void onClick(View v) {
        if (mOnclickListner != null) {
            mOnclickListner.onClick(v, item, applicantName);
        }
    }
}